'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import maya.OpenMaya as OM


'''
============================================================
--->>   UTILITY METHODS
============================================================
'''
def selectByType( inTraversal=OM.MItDag.kDepthFirst, inType=None ):
    '''
    Select all nodes of the given type.
    
    @inTraversal: MItDag::TraversalType. Valid options are...
            kInvalidType - 
            kDepthFirst - 
            kBreadthFirst - 
    @inType: MFn::Type. Read the Maya documentation to get a list of valid
            node types.
    '''
    if inType:
        selList = OM.MSelectionList()
        dagIterator = OM.MItDag( inTraversal, inType )
        while not dagIterator.isDone():
            selList.add( dagIterator.fullPathName() )
            dagIterator.next()
        if not selList.isEmpty():
            OM.MGlobal.setActiveSelectionList( selList )
        else:
            OM.MGlobal.displayInfo( 'No object(s) of type [{0}] exist in this scene.'.format( inType ) )
    else:
        raise RuntimeError( 'inType is None.' )
    
def getByType( inTraversal=OM.MItDag.kDepthFirst, inType=None ):
    '''
    Returns a list of all nodes of the given type.
    
    @inTraversal: MItDag::TraversalType. Valid options are...
            kInvalidType - 
            kDepthFirst - 
            kBreadthFirst - 
    @inType: MFn::Type. Read the Maya documentation to get a list of valid
            node types.
    @return: List of List. Returns a list of all items of the given type. The
            fullpath name and MObject for each item is returned. The format is
            [ [ fullpath name, MObject ] ]
    '''
    if inType:
        selList = []
        dagIterator = OM.MItDag( inTraversal, inType )
        while not dagIterator.isDone():
            selList.append( [ dagIterator.fullPathName(), dagIterator.currentItem() ] )
            dagIterator.next()
        if selList:
            return selList
        else:
            OM.MGlobal.displayInfo( 'No object(s) of type [{0}] exist in this scene.'.format( inType ) )
    else:
        raise RuntimeError( 'inType is None.' )

def selectByName( inName=None, inListAdjust=OM.MGlobal.kReplaceList ):
    '''
    Get all objects with desired name. Good for namespaces or nested hierarchies.
    Takes wild cards in the string.
    
    @inName: String. Name to search the scene for.
    @inListAdjust: MGlobal::listAdjustment. Options are...
            kReplaceList - Totally replace the list with the given items.
            kXORWithList - Any of the items which are already on the list will be removed.
                           Any which are not already on the list will be added to the end of the list.
            kAddToList - Add the items to the end of the list.
            kRemoveFromList - Remove the items from the list.
            kAddToHeadOfList - Add the items to the beginning of the list.
    '''
    if inName:
        try:
            OM.MGlobal.selectByName( inName, inListAdjust )
        except:
            return None
        
selectByType( inTraversal=OM.MItDag.kDepthFirst, inType=OM.MFn.kMesh )